%% Simple calibration Vayanos and Vila 2021
% Kristy A.E. Jansen

clear all; clc; format longG;

% B4_VV_yieldcurve.m
% This do-file calibrates a simple version of VV 2021 based on Internet
% Appendix IA4 of Jansen (2024).
% Output: Figures IA3 and IA4

%% Load Data
scale=100;
yields = readmatrix('Data/DutchGovernmentBondYields.xlsx','Sheet','DailyAll');
DateD = yields(:,1)+693960;
yields = yields(:,2:end)/scale;

%% Dates analysis
% The reform was implemented and announced on 7/2/12 (row 3698)
yields_after = yields(3703,:); 
yields = yields(1:3693,:); 

%% Unconditional means and variances
mu=mean(yields)';
sigma=std(yields)';

%% Conditional mean and variance
% row       row
% 16. 3m     8. 7y 
% 15. 6m     7. 8y
% 14. 1y     6. 9y
% 13. 2y     5. 10y
% 12. 3y     4. 15y
% 11. 4y     3. 20y
% 10. 5y     2. 29y
% 9.  6y     1. 30y

h = [1; 3; 5; 10; 15; 20; 30];
idx = [14; 12; 10; 5; 4; 3; 1];
K=length(h); iota=ones(K,1);

%VAR
AR = {diag(nan(1,K))};
Mdl = varm('AR',AR);
temp=[yields(:,idx)];
EstMdl = estimate(Mdl,temp);
summarize(EstMdl)

%varcovar
uncond_sigma = sigma(idx,1);
for k=1:K
    for j=1:K
correl_est(k,j) = corr(yields(:,idx(k)),yields(:,idx(j)));
    end
end

varcovar_und =  correl_est.* (uncond_sigma * uncond_sigma');

%Daily VAR to annual
c=252;
covar=EstMdl.Covariance*c;


%% Change yield UFR
%Source: wealth hedge funds managers determines arbitrage capital: https://financieel-management.nl/artikelen/cijfers-dnb-vermogen-hedgefondsen-stijgt-opnieuw/
%Also here: https://www.bnr.nl/nieuws/beurs/10167851/pensioenbeheerders-spekken-hedgefondsen
%Total debt is from Dutch Treasurer April 2012: https://english.dsta.nl/documents/publication/2017/02/14/april-2012.

wealth_arb=19.8/280;
wealth_arb_alt = 82.6/280;

ct=[0;0;0;0;0;-0.26;0.22];

gamma=3;

correct1=h-1;
correct2=1./h;

change_yields = (covar.*(correct1*correct1'))*(ct)*gamma/wealth_arb;
change_yields = change_yields.*correct2;

change_yields_alt = (covar.*(correct1*correct1'))*(ct)*gamma/wealth_arb_alt;  
change_yields_alt = change_yields_alt.*correct2;

%% Figure IA3: Calibration of the effect of the regulatory reform on the yield curve

yields_preUFR = yields(end,idx)'*scale;
yields_afterUFR = yields(end,idx)'*scale+change_yields*scale;
yields_afterUFR_alt = yields(end,idx)'*scale+change_yields_alt*scale;

yields_obs_after = yields_after(end,idx)'*scale;

hh = (1:1:30)';

vq1 = interp1(h,yields_preUFR,hh);
vq2 = interp1(h,yields_afterUFR,hh);
vq3 = interp1(h,yields_afterUFR_alt,hh);

%Calibrated yield curve
figure;
plot(hh, smooth(vq1) , 'k',hh, smooth(vq2),'r-.',hh,smooth(vq3),'b :','linewidth',3);
grid on
ylim([1.25 3.25])
xlim([5 30])
xlab=xlabel('Maturity');set(xlab,'FontSize',14,'FontWeight','normal','fontname','Bookman Old Style')
ylab=ylabel('Yield');set(ylab,'FontSize',14,'FontWeight','normal','fontname','Bookman Old Style')
leg=legend('Yield curve pre UFR','Yield curve UFR (arbitrageur wealth 7.07%)','Yield curve UFR (arbitrageur wealth 29.5%)'); set(leg,'FontSize',14,'Location','South','fontname','Bookman Old Style'); legend('boxoff')

set(gca,'FontSize',13)

%% figure yield curve before and after

vq1 = interp1(h,yields_preUFR,hh);
vq2 = interp1(h,yields_obs_after,hh);
vq3 = interp1(h,yields_obs_after - yields_preUFR,hh);

figure;
yyaxis left
plot(hh, smooth(vq1) , 'k',hh, smooth(vq2),'r-.','linewidth',3)
ylim([0.0 3])
ylab=ylabel('Yield');set(ylab,'FontSize',14,'FontWeight','normal','fontname','Bookman Old Style')
yyaxis right
plot(hh,smooth(vq3), 'b :', 'linewidth',3)
ylim([-0.4 0.3])
grid on
xlim([5 30])
yline(0,'-')
xlab=xlabel('Maturity');set(xlab,'FontSize',14,'FontWeight','normal','fontname','Bookman Old Style')
ylab=ylabel('Yield Change');set(ylab,'FontSize',14,'FontWeight','normal','fontname','Bookman Old Style')
leg=legend('Yield curve pre UFR','Yield curve after UFR', 'Yield curve change'); set(leg,'FontSize',14,'Location','SouthEast','fontname','Bookman Old Style'); legend('boxoff')

set(gca,'FontSize',13)







